﻿namespace Arms.Application.Services
{
    using Arms.Domain;
    using Arms.Framework.Validators;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Base class for all application layer services.
    /// </summary>
    public class ServiceBase : IServiceBase
    {
        private readonly IUnitOfWork _uow;

        protected IUnitOfWork UnitOfWork
        {
            get { return _uow; }
        }
        /// <summary>
        /// Arms unit of work.
        /// </summary>
        /// <param name="uow"></param>
        public ServiceBase(IUnitOfWork uow)
        {
            _uow = Validation.Ensure(uow);
        }
        /// <summary>
        /// Gets a collection of list items based on entities with name.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected virtual IEnumerable<ListItemDTO> GetList<T>() where T : EntityDescribed
        {
            return _uow.Repository<T>().Get().Select(x => new ListItemDTO()
            {
                ItemID = x.ID,
                ItemName = x.Name
            });
        }
        #region IDisposable Members

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!_disposed)
                {
                    _uow.Dispose();
                }
                _disposed = true;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(true);
        }

        #endregion
    }
}
